home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / MODEMPRO / TVSERIAL.ZIP;1 / TVSERIAL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-12  |  9.1 KB  |  406 lines

  1. //------------------------------------------------------------------------
  2. //
  3. // Turbo Vision Serial Communications Example
  4. //
  5. //------------------------------------------------------------------------
  6.  
  7. #define Uses_TKeys
  8. #define Uses_TRect
  9. #define Uses_TEvent
  10. #define Uses_TButton
  11. #define Uses_TDialog
  12. #define Uses_TMenuBar
  13. #define Uses_TSubMenu
  14. #define Uses_TMenuItem
  15. #define Uses_TStaticText
  16. #define Uses_TDeskTop
  17. #define Uses_MsgBox
  18. #define Uses_TApplication
  19. #define Uses_TScroller
  20. #define Uses_TTerminal
  21. #include <tv.h>
  22.  
  23. #include <iostream.h>
  24. #include <fstream.h>
  25. #include <strstrea.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <gadgets.h>
  29. #pragma hdrstop
  30.  
  31. #include <conio.h>
  32. #include <ctype.h>
  33.  
  34. #include "tvserial.h"
  35. #include "serial.h"
  36. #include "setup.h"
  37.  
  38. extern "C"
  39. {
  40.     int getccb(void);
  41.     int setSerial(int port, int speed, int parity, int bits, int stopBit);
  42.     int serialOut(char byte);
  43.     void initSerial(void);
  44.     void closeSerial(void);
  45. }
  46. extern int SError;
  47.  
  48.  
  49. comData settings(B2400, COM2, P_NONE, 1, 8);
  50.  
  51. #define INTRO "Turbo Vision Serial Communications Example\n...You're now in terminal mode...\n\n"
  52.  
  53. void main(void)
  54. {
  55.     tvserial terminal;
  56.  
  57.     terminal.run();
  58. }
  59.  
  60.  
  61. //
  62. // ---------------- Functions for class "tvserial" -----------------------
  63. //
  64. tvserial::tvserial() :
  65.     TProgInit( initStatusLine,
  66.                initMenuBar,
  67.                initDeskTop)
  68. {
  69.     TRect r = getExtent();
  70.     r.a.y = r.b.y - 1;
  71.     r.a.x = r.b.x - 13;
  72.     heap = new THeapView( r );
  73.     insert( heap );
  74. }
  75.  
  76. TMenuBar *tvserial::initMenuBar( TRect r )
  77. {
  78.     r.b.y =  r.a.y + 1;
  79.  
  80.     return (new TMenuBar( r,
  81.         *new TSubMenu( "~W~hy?", kbNoKey ) +
  82.             *new TMenuItem( "~A~bout...", cmAboutBox, kbNoKey ) +
  83.  
  84.             *new TMenuItem( "~S~ettings...", cmComSetup, kbNoKey ) +
  85.             *new TMenuItem( "~T~erminal", cmTerminal, kbNoKey ) +
  86.              newLine() +
  87.             *new TMenuItem( "E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X" )
  88.         )
  89.     );
  90. }
  91.  
  92. void tvserial::handleEvent( TEvent& event )
  93. {
  94.     TApplication::handleEvent(event);
  95.  
  96.     if (event.what == evCommand)
  97.         {
  98.         switch (event.message.command)
  99.             {
  100.             case cmAboutBox:
  101.                 aboutDlgBox();
  102.                 break;
  103.             case cmComSetup:
  104.                 comSetup();
  105.                 break;
  106.             case cmTerminal:
  107.                 terminal();
  108.                 break;
  109.             }
  110.         }
  111. }
  112.  
  113.  
  114. void tvserial::aboutDlgBox()
  115. {
  116.     TDialog *aboutBox = new TDialog(TRect(0, 0, 39, 13), "About");
  117.  
  118.     aboutBox->insert(
  119.       new TStaticText(TRect(1, 2, 38, 9),
  120.         "\003Turbo Vision\n"
  121.         "\003Serial Communications\n"
  122.         "\003Example\n \n"                    
  123.         "\003Copyright (c) 1991\n \n"         
  124.         "\003Borland International"
  125.         )
  126.     );
  127.  
  128.     aboutBox->insert(
  129.       new TButton(TRect(14, 10, 25, 12), " OK", cmOK, bfDefault)
  130.     );
  131.  
  132.     aboutBox->options |= ofCentered;
  133.     deskTop->execView(aboutBox);
  134.  
  135.     destroy(aboutBox);
  136. }
  137.  
  138.  
  139. void tvserial::terminal()
  140. {
  141.     TTermWindow *t = (TTermWindow *) validView( new TTermWindow() );
  142.     if( t != 0 )
  143.         deskTop->insert( t );
  144. }
  145.  
  146. void tvserial::comSetup()
  147. {
  148.     TSetupDialog *t = (TSetupDialog *) validView( new TSetupDialog() );
  149.  
  150.     if( t != 0 )
  151.     {
  152.         t->options |= ofCentered;
  153.         t->setData( (void *) &settings);
  154.         if( deskTop->execView( t ) != cmCancel )
  155.             t->getData( (void *) &settings);
  156.     }
  157.  
  158.     destroy(t);
  159. }
  160.  
  161.  
  162. //
  163. // TTermWindow functions
  164. //
  165. TTermWindow::TTermWindow() :
  166.     TWindow( TProgram::deskTop->getExtent(), "So Red the Rose", 1 ),
  167.     TWindowInit( initFrame )
  168. {
  169.     TRect r( getExtent() );
  170.     r.grow(-1, -1);
  171.     options |= ofTileable;
  172.     TTermView *t = new TTermView( r,
  173.                          standardScrollBar(sbHorizontal | sbHandleKeyboard),
  174.                          standardScrollBar(sbVertical | sbHandleKeyboard)
  175.                        );
  176.     if( t != 0 )
  177.         insert(t);
  178. }
  179.  
  180.  
  181. TTermView *TTermView::instance;
  182.  
  183. void TTermView::update()
  184. {
  185.     int c;
  186.  
  187.     if( instance != 0 &&
  188.         (c = getccb()) != -1 &&
  189.         c != '\n' && c != '\f' && c != '\0'
  190.       )
  191.     {
  192.         if( c == '\r' )
  193.             c = '\n';
  194.         instance->do_sputc( (char) c );
  195.         instance->drawView();
  196.     }
  197. }
  198.  
  199.  
  200. TTermView::TTermView( const TRect& bounds,
  201.                       TScrollBar *aHScrollBar,
  202.                       TScrollBar *aVScrollBar
  203.                     ) :
  204.     TTerminal( bounds, aHScrollBar, aVScrollBar, 32768U )
  205. {
  206.     successFlag = True;
  207.  
  208.     if( instance != 0 )
  209.     {
  210.         messageBox("Already have a terminal open.", mfOKButton);
  211.         successFlag = False;
  212.     }
  213.     else
  214.         instance = this;
  215.  
  216.     if(setSerial(settings.getComPort(), settings.getBaud(),
  217.                  settings.getParity(), settings.getDataBits(),
  218.                  settings.getStopBits()) != 0)
  219.     {
  220.         messageBox("Serial Port setup error.", mfOKButton);
  221.         successFlag = False;
  222.     }
  223.  
  224.     initSerial();
  225.  
  226.     do_sputn( INTRO, strlen(INTRO) );
  227. }
  228.  
  229. TTermView::~TTermView()
  230. {
  231. //    char buf[64];
  232.  
  233.     instance = 0;
  234.  
  235.     switch (SError)
  236.     {
  237.         case NOERROR:
  238. //            do_sputn("\nbye.\n", 6);
  239. //            drawView();
  240.             break;
  241.         case BUFOVFL:
  242. //            do_sputn("\nBuffer Overflow.\n", 18);
  243. //            drawView();
  244.             break;
  245.         default:
  246. //            ostrstream(buf, sizeof(buf)) << "\nUnknown Error, SError = " << SError << '\n' << ends;
  247. //            do_sputn(buf, strlen(buf));
  248. //            drawView();
  249.             break;
  250.     }
  251.     closeSerial();
  252. }
  253.  
  254. void TTermView::handleEvent( TEvent& event )
  255. {
  256.     TTerminal::handleEvent( event );
  257.  
  258.     if( event.what == evKeyboard &&
  259.         event.keyDown.charScan.charCode != 0 &&
  260.         isascii( event.keyDown.charScan.charCode )
  261.       )
  262.     {
  263.         serialOut( event.keyDown.keyCode );
  264.         clearEvent( event );
  265.     }
  266. }
  267.  
  268. void TTermView::draw()
  269. {
  270.     short  i;
  271.     ushort begLine, endLine;
  272.     char s[256];
  273.     ushort bottomLine;
  274.  
  275.     bottomLine = size.y + delta.y;
  276.     if( limit.y > bottomLine )
  277.     {
  278.         endLine = prevLines( queFront, limit.y - bottomLine );
  279.         bufDec( endLine );
  280.     }
  281.     else
  282.         endLine = queFront;
  283.  
  284.     if( limit.y > size.y )
  285.         i = size.y - 1;
  286.     else
  287.     {
  288.         for( i = limit.y; i <= size.y - 1; i++ )
  289.             writeChar(0, i, ' ', 1, size.x);
  290.         i =  limit.y -  1;
  291.     }
  292.  
  293.     for( ; i >= 0; i-- )
  294.     {
  295.         begLine = prevLines(endLine, 1);
  296.         if (endLine >= begLine)
  297.         {
  298.             int T = (int) (endLine - begLine);
  299.             memcpy( s, &buffer[begLine], T );
  300.             s[T] = EOS;
  301.         }
  302.         else
  303.         {
  304.             int T = (int) (bufSize - begLine);
  305.             memcpy( s, &buffer[begLine], T );
  306.             memcpy( s+T, buffer, endLine );
  307.             s[T+endLine] = EOS;
  308.         }
  309.         if( delta.x >= strlen(s) )
  310.             *s = EOS;
  311.         else
  312.             strcpy( s, &s[delta.x] );
  313.  
  314.         writeStr( 0, i, s, 1 );
  315.         writeChar( strlen(s), i, ' ', 1, size.x );
  316.         endLine = begLine;
  317.         bufDec( endLine );
  318.     }
  319. }
  320.  
  321.  
  322. void TTermView::do_sputc( const char c )
  323. {
  324.     ushort screenLines = limit.y;
  325.     int i;
  326.  
  327.     if( c == '\n' )
  328.         screenLines++;
  329.  
  330.     if( !canInsert( 1 ) )
  331.     {
  332.         queBack = nextLine( queBack );
  333.         screenLines--;
  334.     }
  335.  
  336.     if( c != '\x08' )
  337.     {
  338.         buffer[queFront++] = c;
  339.         if( queFront == bufSize )
  340.             queFront = 0;
  341.     }
  342.     else if( queFront != queBack )
  343.     {
  344.         --queFront;
  345.         if( (int) queFront < 0 )
  346.             queFront += bufSize;
  347.         if( buffer[queFront] == '\n' )
  348.             --screenLines;
  349.     }
  350.  
  351.     ++drawLock;
  352.     setLimit( limit.x, screenLines );
  353.     scrollTo( 0, screenLines + 1 );
  354.     --drawLock;
  355.  
  356.     i = prevLines( queFront, 1 );
  357.     if( i <= queFront )
  358.         i = queFront - i;
  359.     else
  360.         i = bufSize - (i - queFront);
  361.     setCursor( i, screenLines - delta.y - 1 );
  362.     return;
  363. }
  364.  
  365.  
  366. int TTermView::do_sputn( const char *s, int count )
  367. {
  368.     ushort screenLines = limit.y;
  369.     for( ushort i = 0; i < count; i++ )
  370.         if( s[i] == '\n' )
  371.             screenLines++;
  372.  
  373.     while( !canInsert( count ) )
  374.         {
  375.         queBack = nextLine( queBack );
  376.         screenLines--;
  377.         }
  378.  
  379.     if( queFront + count >= bufSize )
  380.         {
  381.         i = bufSize - queFront;
  382.         memcpy( &buffer[queFront], s, i );
  383.         memcpy( buffer, &s[i], count - i );
  384.         queFront = count - i;
  385.         }
  386.     else
  387.         {
  388.         memcpy( &buffer[queFront], s, count );
  389.         queFront += count;
  390.         }
  391.  
  392.     ++drawLock;
  393.     setLimit( limit.x, screenLines );
  394.     scrollTo( 0, screenLines + 1 );
  395.     --drawLock;
  396.  
  397.     i = prevLines( queFront, 1 );
  398.     if( i <= queFront )
  399.         i = queFront - i;
  400.     else
  401.         i = bufSize - (i - queFront);
  402.     setCursor( i, screenLines - delta.y - 1 );
  403.     return count;
  404. }
  405.  
  406.